import { createStore } from 'vuex'
import router from '../router'
import {Properties as tabs, Properties as e} from "svg-sprite-loader/examples/custom-runtime-generator/build/main";

export default createStore({
  state: {
    hasRoutes: false,
      // 存储动态tabs
      editableTabsValue: '/index' ,
      editableTabs: [
          {
              title:'首页',
              name:'/index',
          }

      ]
  },
  // 获取存储的数据
  getters: {
    GET_TOKEN(state){ return sessionStorage.getItem('token') },
    GET_MENULIST(state){ return JSON.parse(sessionStorage.getItem('menuList')) },
    GET_USERINFO(state){ return JSON.parse(sessionStorage.getItem('userInfo')) },
  },
  // 修改存储的数据
  mutations: {
    SET_TOKEN(state, token) {
        sessionStorage.setItem('token', token);
    },

    SET_MENULIST(state, menuList) {
            sessionStorage.setItem('menuList', JSON.stringify(menuList));
    },
    SET_USERINFO(state, userInfo) {
                sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
    },
    SET_HAS_ROUTES(state, hasRoutes) {
        state.hasRoutes = hasRoutes;
    },
    ADD_TABS(state, tabs) {
      // 遍历每一项，如果标签数组中没有，就添加到数组中
      if (state.editableTabs.findIndex(e=> e.name === tabs.path) === -1){
          state.editableTabs.push({
              title: tabs.name,
              name: tabs.path
          });
      }
      // 添加成功，顺便切换到新增的标签上去
        state.editableTabsValue = tabs.path;
    },
      // 重置标签：回到首页呗
      RESET_TABS(state) {
        state.editableTabsValue = '/index';
        state.editableTabs = [{
            title: '首页',
            name: '/index'
        }]
      }
  },
  actions: {
    logout() {
        window.sessionStorage.clear();
        router.replace('/login');
    }
  },
  modules: {
  }
})
